home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / borland / jnfb88.zip / USETC.ZIP / CONCAT.C next >
Text File  |  1987-05-17  |  3KB  |  77 lines

  1. /********************************************************************
  2. *  C O N C A T
  3. *
  4. *  Concatenate files.  For each file named as an argument, CONCAT
  5. *  writes the contents of the file to standard output.  Command-line
  6. *  redirection may be used to collect the contents of multiple files
  7. *  into a single file.  This program is adapted for DOS from the
  8. *  "cat" program presented in "The C Programming Language", by
  9. *  Kernighan and Ritchie, Prentice-Hall, 1978.  Modifications include
  10. *  argv[0] processing for DOS and improved error handling.
  11. *
  12. *  Exitcodes (DOS ERRORLEVEL):
  13. *    0    success
  14. *    1    error opening a named file
  15. *    2    I/O error while copying
  16. *    3    error closing a file
  17. ********************************************************************/
  18.  
  19. #include <stdio.h>
  20.  
  21. /* function prototype */
  22. extern int filecopy(FILE *, FILE *);
  23.  
  24. main(argc, argv)
  25. int argc;
  26. char *argv[];
  27. {
  28.     int i;        /* loop index */
  29.     FILE *fp;        /* input file pointer */
  30.  
  31.     static char progname[] = { "CONCAT" };    /* program name */
  32.  
  33.     /*
  34.      *  Be sure that argv[0] is a useful program name.  Under DOS 3.x
  35.      *  and later, argv[0] is the program name.  The program name is
  36.      *  not available under earlier versions of DOS and is presented
  37.      *  as a null string ("") by Turbo C.
  38.      */
  39.     if (argv[0][0] == '\0')
  40.         argv[0] = progname;
  41.  
  42.     /* if no filenames are given, use standard input */
  43.     if (argc == 1) {
  44.         if (filecopy(stdin, stdout) == EOF) {
  45.             perror(argv[0]);    /* display the system error message */
  46.             exit(2);
  47.         }
  48.     }
  49.     else
  50.         /* process the named files one at a time */
  51.         for (i = 1; i < argc; ++i) {
  52.             /* attempt to open the source file */
  53.             fp = fopen(argv[i], "r");
  54.             if (fp == NULL) {
  55.                 /* unable to open the file */
  56.                 fprintf(stderr, "%s: cannot open %s\n",
  57.                     argv[0], argv[i]);
  58.                 continue;    /* look for more files */
  59.             }
  60.             else {
  61.                 /* copy the current file to the standard output */
  62.                 if (filecopy(fp, stdout) == EOF) {
  63.                     perror(argv[0]);
  64.                     exit(2);
  65.                 }
  66.                 /* close the current file */
  67.                 if (fclose(fp) == EOF) {
  68.                     fprintf(stderr, "%s: error closing %s\n",
  69.                         argv[0], argv[i]);
  70.                     exit(3);
  71.                 }
  72.             }
  73.         }
  74.  
  75.     exit(0);
  76. }
  77.